<template>
    <div>
        <el-dialog :title="(type == 0 ? '添加管理员信息' : '编辑管理员信息')" :visible="visible" :before-close="handleClose">
            <div v-if="(type == 0)">
                <el-form :model="form">
                    <el-form-item label="管理员姓名" label-width="120px">
                        <el-input v-model="form.adminName" autocomplete="on"></el-input>
                    </el-form-item>
                    <el-form-item label="性别" label-width="120px">
                        <el-switch v-model="form.adminSex" active-color="#FE12FE" inactive-color="#00FFFF"
                            active-value="1" inactive-value="0" active-text="女" inactive-text="男">
                        </el-switch>
                    </el-form-item>
                    <el-form-item label="电话号码" label-width="120px">
                        <el-input v-model="form.adminPhone" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱地址" label-width="120px">
                        <el-input v-model="form.adminEmail" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="handleClose">取 消</el-button>
                    <el-button type="primary" @click="saveAdmin">添加</el-button>
                </div>
            </div>
            <div v-else>
                <el-form :model="formData">
                    <el-form-item label="管理员姓名" label-width="120px">
                        <el-input v-model="formData.adminName" autocomplete="on"></el-input>
                    </el-form-item>
                    <el-form-item label="性别" label-width="120px">
                        <el-switch v-model="formData.adminSex" active-color="#FE12FE" inactive-color="#00FFFF"
                            active-value="1" inactive-value="0" active-text="女" inactive-text="男">
                        </el-switch>
                    </el-form-item>
                    <el-form-item label="电话号码" label-width="120px">
                        <el-input v-model="formData.adminPhone" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱地址" label-width="120px">
                        <el-input v-model="formData.adminEmail" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="handleClose">取 消</el-button>
                    <el-button type="primary" @click="updateAdmin">修改</el-button>
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { save } from '@/api/adminApi';
const options = {
    props: ["visible", "type", "formData"],
    data() {
        return {
            form: {
                adminName: "",
                adminId: "",
                adminPhone: "",
                adminEmail: "",
                adminPWD: "",
                adminSex: 0
            }
        }
    },
    methods: {
        handleClose() {
            this.$confirm('确认关闭？')
                .then(() => {
                    this.close();
                })
                .catch(() => { });
        },
        close() {
            // console.log("关闭");
            this.$emit('close', null);
        },
        saveAdmin() {
            let that = this;
            save(this.form)
                .then((resp) => {
                    if (resp.data.code !== 0) {
                        that.$message.success("添加管理员成功");
                    } else {
                        that.$message.error("添加管理员失败");
                    }
                }).catch()
            this.close();
        },
        updateAdmin() {

            this.close();
        }
    }
}

export default options;
</script>